package com.itheima.springbootinittest10.controller;

import com.itheima.springbootinittest10.pojo.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @RequestMapping("/hello")//http://127.0.0.1:8080/hello
    public String hello(){
        return "hello spring boot 10!";
    }
    @Value("${name}")
    private String name;
    @Value("${person.age}")
    private Integer personAge;
    @Value("${address[0]}")
    private String address;
    @Value("${content1}")
    private String content1;
    @Value("${content2}")
    private String content2;
    @Autowired //相当于创建了一个env对象
    private Environment env;
    @Autowired //相当于创建了一个person对象
    private Person person;

    @RequestMapping("/hello2")//http://127.0.0.1:8080/hello2
    public String hello2(){
        System.out.println("name is:"+name);//lisi
        System.out.println("person.age is:"+personAge);//18
        System.out.println("address0 is:"+address);//18
        System.out.println("内容1"+content1);//换行了
        System.out.println("内容2"+content2);//不换行
        //###################################
        System.out.println("env的name内容是："+env.getProperty("name"));//Environment形式的用法
        System.out.println("env的address内容是："+env.getProperty("address[0]"));//Environment形式的用法
        //###################################
        //System.out.println("person对象从配置文件中获取一级属性："+person);//Person{name='lisi', age=null}
        System.out.println("person对象从配置文件中获取2级属性："+person);//Person{name='lisi', age=18}
        String[] addresses=person.getAddress();
        for (String s : addresses) {
            System.out.println("address is:"+ s);
        }
        return "hello spring boot 11!";
    }
}
